// modeless dialog manager, added by regret

/* changelog:
 update 2: add dialogInfo
 update 1: create
*/

#include "burner.h"
#include "dialogmanager.h"

DialogManager dialogManager;

DialogManager::DialogManager()
{
	dialogMap.clear();
}

DialogManager::~DialogManager()
{
	dialogMap.clear();
}

void DialogManager::add(int id, HWND dialog, int flag)
{
	remove(id);
	DialogInfo info(dialog, flag);
	dialogMap[id] = info;
}

HWND DialogManager::get(int id)
{
	map<int, DialogInfo>::iterator iter = dialogMap.find(id);
	if (iter != dialogMap.end()) {
		return iter->second.hwnd;
	}
	return NULL;
}

void DialogManager::remove(int id)
{
	map<int, DialogInfo>::iterator iter = dialogMap.find(id);
	if (iter != dialogMap.end()) {
		::DestroyWindow(iter->second.hwnd);
		dialogMap.erase(iter);
	}
}

bool DialogManager::isEmpty()
{
	return dialogMap.empty();
}

void DialogManager::clear()
{
	map<int, DialogInfo>::iterator iter = dialogMap.begin();
	for (; iter != dialogMap.end(); iter++) {
		::DestroyWindow(iter->second.hwnd);
	}
	dialogMap.clear();
}

bool DialogManager::isDlgMessage(MSG* msg)
{
	if (!msg) {
		return false;
	}

	map<int, DialogInfo>::iterator iter = dialogMap.begin();
	for (; iter != dialogMap.end(); iter++) {
		if (::IsDialogMessage(iter->second.hwnd, msg)) {
			return true;
		}
	}
	return false;
}
